#!/usr/bin/env python
# -*- coding: utf-8 -*-

import  time

from foxglove import log
from foxglove.juclient import CacheClient


#######################################################################

HOST = "127.0.0.1"
PORT = 54344

SERVICE = 'CACHE'

def main(options):
    if options.verbose :
        log.enableDebug()
    else :
        log.disableDebug()
    if options.times > 0 :
        times = options.times
    else :
        times = 10

    cache = CacheClient(SERVICE, [(HOST,PORT),])

    time_start = time.time()
    
    for i in range(times) :
        key = 'key--test--%08d' % i
        data = 'data--test--%08d' % i
        timeout = 10
        try :
            status = cache.insert(key, data, timeout)
        except :
            log.excpt()
        else :
            if status != 200 and status != 201 :
                log.error('Fail to insert (key=%s, data=%s, timeout=%d): status=%d', `key`, `data`, timeout, status)
            elif status == 201 :
                if log.debugable : log.debug('Ignore insertion (key=%s, data=%s, timeout=%d).', `key`, `data`, timeout)

    for i in range(times) :
        key = 'key--test--%08d' % i
        data = 'data--test--%08d' % i
        timeout = 1
        try :
            status,value = cache.lookup(key)
        except :
            log.excpt()
        else :
            if status != 200 and status != 202 :
                log.error('Fail to lookup (key=%s): status=%d', `key`, status)
            elif status == 202 :
                if log.debugable : log.debug('Expired to lookup (key=%s).', `key`)
            elif data != value :
                log.error('Fail to lookup (key=%s): value=%s expect=%s', `key`, `value`, `data`)
                
    time_stop = time.time()
    del cache
    
    time_last = time_stop - time_start
    tps = times / time_last
    log.info('times=%d time-last:%.2f seconds tps=%.2f', times, time_last, tps)
    # main()


if __name__ == '__main__' :
    from optparse import OptionParser
    parser = OptionParser()
    parser.add_option("-v", "--verbose", dest="verbose", action="store_true", default=False,
                        help='show detail information')
    parser.add_option("", "--times", dest="times", type="int", default=10,
                        help='times of ping-pong between beater and echoer')
    parser.add_option("", "--bytes", dest="bytes", type="int", default=0,
                        help='bytes of message for ping-pong between beater and echoer')
    parser.add_option("", "--profile", dest="profile", action="store_true", default=False,
                        help='show profile information')
    (options, args) = parser.parse_args()
    if options.profile :
        prof_file = 'prof.beat.txt'
        import cProfile
        cProfile.run("main(options)", prof_file)
        import pstats
        p = pstats.Stats(prof_file)
        #p.sort_stats("time").print_stats()
        p.sort_stats("cumulative").print_stats()
    else :
        main(options)

    import sys
    sys.exit(0)
